package it.osg.openstory.web;

import it.osg.openstory.model.User;
import it.osg.openstory.services.AuthenticationService;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.json.JSONObject;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class VerifyMailAction extends ActionSupport implements
		ServletRequestAware, ServletResponseAware {

	private AuthenticationService mailService;
	@SuppressWarnings("unused")
	private HttpServletRequest request;
	@SuppressWarnings("unused")
	private HttpServletResponse response;

	@Override
	public String execute() throws Exception {

		String mail = request.getParameter("mail");
		User userToVerify = new User();
		userToVerify.setMail(mail);
		User userFound = mailService.findUserByMail(userToVerify);

		JSONObject obj = new JSONObject();
		if (userFound == null)
			obj.put("mail", "notAMAil");
		else
			obj.put("mail", userFound.getMail());

		PrintWriter out = response.getWriter();
		out.println(obj);
		out.flush();
		out.close();

		return SUCCESS;
	}

	public AuthenticationService getMailService() {
		return mailService;
	}

	public void setMailService(AuthenticationService mailService) {
		this.mailService = mailService;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

}
